import express from 'express';
import { generateAvatarImage, getAvatarTemplates, getAvatarTemplate } from '../services/avatarService.js';
import { validateAvatarRequest } from '../middleware/validation.js';

const router = express.Router();

// 生成头像
router.post('/generate', validateAvatarRequest, async (req, res, next) => {
  try {
    const { type, gender, expression, age, customDescription, personality } = req.body;
    
    console.log('🎨 收到头像生成请求:', { type, gender, expression, age, customDescription, personality });
    
    const result = await generateAvatarImage({
      type,
      gender,
      expression,
      age,
      customDescription,
      personality
    });
    
    res.json({
      success: true,
      data: result,
      message: '头像生成成功'
    });
  } catch (error) {
    next(error);
  }
});

// 获取所有头像模板
router.get('/templates', async (req, res, next) => {
  try {
    const templates = getAvatarTemplates();
    res.json({
      success: true,
      data: templates,
      message: '获取模板成功'
    });
  } catch (error) {
    next(error);
  }
});

// 根据类型和性别获取特定模板
router.get('/templates/:type/:gender', async (req, res, next) => {
  try {
    const { type, gender } = req.params;
    const template = getAvatarTemplate(type, gender);
    
    if (!template) {
      return res.status(404).json({
        success: false,
        message: '未找到对应的模板'
      });
    }
    
    res.json({
      success: true,
      data: template,
      message: '获取模板成功'
    });
  } catch (error) {
    next(error);
  }
});

// 获取头像生成状态
router.get('/status/:jobId', async (req, res, next) => {
  try {
    const { jobId } = req.params;
    // 这里可以实现任务状态查询逻辑
    res.json({
      success: true,
      data: {
        jobId,
        status: 'completed',
        progress: 100
      },
      message: '获取状态成功'
    });
  } catch (error) {
    next(error);
  }
});

export default router;
